package com.haierCamera.customapplication.ui.main;

import android.content.Context;
import android.content.Intent;
import android.graphics.Outline;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.Filter;
import android.widget.Filterable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.vo.GetDeviceEntity;
import com.haierCamera.customapplication.databinding.HzklAdapterCameraItem2Binding;
import com.haierCamera.customapplication.databinding.HzklAdapterCameraItemBinding;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.custom.BaseListenerOnClik;
import com.haierCamera.customapplication.ui.custom.DataBoundViewHolder;
import com.haierCamera.customapplication.ui.main.widget.DialogEditDevice;
import com.haierCamera.customapplication.ui.mediaplay.MediaPlayActivity;
import com.haierCamera.customapplication.ui.user.activity.HZKLRepairActivity;
import com.haierCamera.customapplication.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by AdministratorZJ
 * on 2019/12/9.
 */
public class CameraDeviceListAdapter extends RecyclerView.Adapter<DataBoundViewHolder<HzklAdapterCameraItem2Binding>> implements Filterable {
    private List<GetDeviceEntity> data;
    private List<GetDeviceEntity> mSourceList = new ArrayList<>();
    private Context context;
    private FragmentActivity activity;
    private BaseListenerOnClik click;
    private LongClickListener longClickListener;
    int type = 0;

    public interface LongClickListener {
        void onLongClick(int i);
    }

    public CameraDeviceListAdapter(Context context) {
        this.context = context;
    }

    public CameraDeviceListAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }

    public void upDate(List<GetDeviceEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setOnItemOnClick(BaseListenerOnClik click) {
        this.click = click;
    }

    public void setLongClickListener(LongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    @Override
    public DataBoundViewHolder<HzklAdapterCameraItem2Binding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        Log.i("TAG","LeaveMessageAdapter_onCreateViewHolder");
        HzklAdapterCameraItem2Binding binding = DataBindingUtil.inflate(LayoutInflater.from(context)
                , R.layout.hzkl_adapter_camera_item2, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<HzklAdapterCameraItem2Binding> holder, int i) {

//        holder.binding.tvDevice.setText("" + data.get(i).name);
////        holder.binding.frameContent.setOutlineProvider(new ViewOutlineProvider() {
////            @Override
////            public void getOutline(View view, Outline outline) {
////                outline.setRoundRect(0,
////                        0,
////                        view.getWidth(),
////                        view.getHeight(),
////                        DensityUtil.dip2px(context,8));
////            }
////        });
////        holder.binding.frameContent.setClipToOutline(true);
////        if (data.get(i).picture!=null && !"".equals(data.get(i).picture)) {
////            RequestOptions requestOptions = new RequestOptions();
////            requestOptions.placeholder(R.drawable.hzkl_media_bg);
////            requestOptions.centerCrop();
////            Glide.with(context).asBitmap()
////                    .load(data.get(i).picture)
////                    .apply(requestOptions)
////                    .into(holder.binding.frameContent);
////        }
////
////
////        holder.binding.ivEditCamera.setOnClickListener((v)->{
////            new DialogEditDevice(context).setListener(new DialogEditDevice.OnSelectedListener() {
////                @Override
////                public void click(int type) {
////                    switch (type) {
////                        case 0:
////                            context.startActivity(new Intent(context
////                                    ,HZKLMsgAlarmSettingActivity.class)
////                                    .putExtra("devId",data.get(i).serialNumber));
////                            break;
////                        case 1:
////                            context.startActivity(new Intent(context
////                                ,HZKLRecordListAllActivity.class)
////                                    .putExtra("devId",data.get(i).serialNumber)
////                                    .putExtra("cameraPwd",data.get(i).securityCode));
////
////                            break;
////                        case 2:
////                            context.startActivity(new Intent(context
////                                    ,HZKLCameraDeviceDetailActivity.class)
////                                    .putExtra("accountDeviceId",data.get(i).accountDeviceId)
////                                    .putExtra("name",data.get(i).name)
////                                    .putExtra("sn",data.get(i).serialNumber)
////                                    .putExtra("sc",data.get(i).securityCode)
////                                    .putExtra("picture",data.get(i).picture));
////                            break;
////                        case 3:
////                            context.startActivity(new Intent(context
////                                    , HZKLRepairActivity.class)
////                                    .putExtra("deviceId",data.get(i).serialNumber));
////                            break;
////                            default:
////                                break;
////                    }
////                }
////            }).showDialog();
////        });
////
////        holder.binding.frameContent.setOnClickListener((v)->{
////
////            click.onClick(i);
//////            getDeviceChannel(data.get(i).serialNumber,i);
////        });

        holder.binding.tvName.setText("" + data.get(i).name);
        if (data.get(i).onLine == 0) {
            holder.binding.tvState.setText("离线");
        } else if (data.get(i).onLine == 1) {
            holder.binding.tvState.setText("在线");
        }
        holder.binding.tvPosition.setText("");

        holder.itemView.setOnClickListener((v)->{
            click.onClick(i);
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new DialogEditDevice(context).setListener(new DialogEditDevice.OnSelectedListener() {
                    @Override
                    public void click(int type) {
                        switch (type) {
                            case 0:
                                context.startActivity(new Intent(context
                                        ,HZKLMsgAlarmSettingActivity.class)
                                        .putExtra("devId",data.get(i).serialNumber));
                                break;
                            case 1:
                                context.startActivity(new Intent(context
                                        ,HZKLRecordListAllActivity.class)
                                        .putExtra("devId",data.get(i).serialNumber)
                                        .putExtra("cameraPwd",data.get(i).securityCode));

                                break;
                            case 2:
                                context.startActivity(new Intent(context
                                        ,HZKLCameraDeviceDetailActivity.class)
                                        .putExtra("accountDeviceId",data.get(i).accountDeviceId)
                                        .putExtra("name",data.get(i).name)
                                        .putExtra("sn",data.get(i).serialNumber)
                                        .putExtra("sc",data.get(i).securityCode)
                                        .putExtra("picture",data.get(i).picture));
                                break;
                            case 3:
                                context.startActivity(new Intent(context
                                        , HZKLRepairActivity.class)
                                        .putExtra("deviceId",data.get(i).serialNumber));
                                break;
                            default:
                                break;
                        }
                    }
                }).showDialog();
                return false;
            }
        });

    }

    public void changeFragment(Fragment targetFragment, boolean isAddToStack) {
        if (isAddToStack) {
            activity.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_content, targetFragment)
                    .addToBackStack(null).commitAllowingStateLoss();
        } else {
            activity.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_content, targetFragment)
                    .commitAllowingStateLoss();
        }

    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    data = mSourceList;
                } else {
                    List<GetDeviceEntity> filteredList = new ArrayList<>();
                    for (GetDeviceEntity str : mSourceList) {
                        //这里根据需求，添加匹配规则
                        if (str.name.contains(charString)) {
                            filteredList.add(str);
                        }
                    }

                    data = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = data;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                data = (ArrayList<GetDeviceEntity>) filterResults.values;
                //刷新数据
                notifyDataSetChanged();
            }
        };
    }

    @Override
    public int getItemCount() {
        if (data!=null) {
            return data.size();
        } else {
            return 0;
        }
    }
}
